package com.bolt.report.engine.calculator;

import com.bolt.common.convert.Convert;
import com.bolt.common.utils.ArrayUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.report.engine.BindData;
import com.bolt.report.engine.Calculator;
import com.bolt.report.engine.fill.FillContext;
import com.bolt.report.engine.model.Cell;
import com.bolt.report.engine.model.Expression;

public class DecodeCalculator implements Calculator {

    @Override
    public Object evaluate(FillContext context, BindData data, Expression.CalculatorExpression expr, Cell cell) {
        String result = StrUtil.EMPTY;
        Object value = data.getData();
        if (ArrayUtil.isNotEmpty(expr.getArgs())) {
            String[] args = expr.getArgs();
            for (int i = 0; i < args.length; ) {
                String key = args[i];
                String item = args[i + 1];
                if (Convert.toStr(value).equals(key)) {
                    result = item;
                    break;
                }
                i = i + 2;
            }
        }
        return result;
    }


}